# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=gnutls
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.5.2
pkgrel=1
pkgdesc="A library which provides a secure layer over a reliable transport layer (mingw-w64)"
arch=('any')
license=('GPL3' 'LGPL2.1')
url="http://www.gnutls.org/"
options=('!zipman')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-libidn"
         "${MINGW_PACKAGE_PREFIX}-libsystre"
         "${MINGW_PACKAGE_PREFIX}-libtasn1"
         "${MINGW_PACKAGE_PREFIX}-nettle>=3.1"
         "${MINGW_PACKAGE_PREFIX}-p11-kit>=0.23.1"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         #"${MINGW_PACKAGE_PREFIX}-unbound"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
# Need remove "autogen" package before building.
source=(ftp://ftp.gnutls.org/gcrypt/gnutls/v${pkgver%.*}/${_realname}-${pkgver}.tar.xz{,.sig}
        0001-add-missing-define.patch
        0002-gnutls-3.4.7-socket.patch
        0003-gnutls-fix-external-libtasn1-detection.patch)
sha256sums=('2b45e95cbfd21d76da6fc4846794d8845dbc66bb03c6aa1428881586cfbe8582'
            'SKIP'
            'fe1d9df924f72ec82972df328467591e49fe82feacebaf2605d37cc059c5429e'
            '043fd01e769efda343783be88f3a27f0cf030b3a2352720fcf6b8cec1eca1930'
            'fa81c4be2cdb3743e42ac75983f713223336e515a59cd299eefa9f7e44da13ee')
validpgpkeys=('0424D4EE81A0E3D119C6F835EDA21E94B565716F' '1F42418905D8206AA754CCDC29EE58B996865171')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/0001-add-missing-define.patch
  patch -p1 -i ${srcdir}/0002-gnutls-3.4.7-socket.patch
  patch -p1 -i ${srcdir}/0003-gnutls-fix-external-libtasn1-detection.patch

  WANT_AUTOMAKE=latest autoreconf -fi -I m4
}

build() {
  # Workaround for localtime_r functions
  CFLAGS+=" -D_POSIX_C_SOURCE"
  CXXFLAGS+=" -D_POSIX_C_SOURCE"

  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --with-zlib \
    --enable-shared --enable-static \
    --enable-cxx \
    --enable-nls \
    --disable-rpath \
    --disable-gtk-doc \
    --with-libiconv-prefix=${MINGW_PREFIX} \
    --with-libregex-libs=-lsystre \
    --enable-local-libopts \
    --disable-guile \
    --disable-libdane \
    --disable-tests \
    gl_cv_double_slash_root=yes

  make
}

check() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make -k check || /bin/true
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" install

  find "${pkgdir}${MINGW_PREFIX}" -name '*.def' -o -name '*.exp' | xargs -rtl1 rm
}
